
using AutoMapper;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Model;
using SqlSugar;
using Util;

namespace API.Controllers
{
    public class Base_DepartmentController : BaseController
    {
        Repository<tBase_Department> _Base_Department;
        readonly IMapper _mapper;
        public Base_DepartmentController(Repository<tBase_Department> Base_Department, IMapper mapper)
        {
            _Base_Department = Base_Department;
            _mapper = mapper;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultMsg<string>> Save([FromBody] SaveBase_DepartmentInput dto)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                var model = _mapper.Map<tBase_Department>(dto);
                InitEntity(model);
                await _Base_Department.InsertOrUpdateAsync(model);
                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Base_DepartmentController--Save:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultMsg<List<Base_DepartmentOutput>>> GetList([FromBody] Base_DepartmentListInput dto)
        {
            ResultMsg<List<Base_DepartmentOutput>> result = new ResultMsg<List<Base_DepartmentOutput>>();
            try
            {
                if (dto.PageNum <= 0)
                {
                    return result.Error("PageNum不能小于或等于0");
                }
                if (dto.PageSize <= 0)
                {
                    return result.Error("PageSize不能小于或等于0");
                }
                SqlSugar.PageModel pageModel = new SqlSugar.PageModel
                {
                    PageIndex = dto.PageNum,
                    PageSize = dto.PageSize,
                };
                var exp = Expressionable.Create<tBase_Department>();
                if (!string.IsNullOrWhiteSpace(dto.Name))
                {
                    exp.And(x => x.FName.Contains(dto.Name));
                }
                var list = (await _Base_Department.GetPageListAsync(exp.ToExpression(), pageModel)).OrderByDescending(x => x.CreateTime).ToList();
                var Base_DepartmentOutputs = _mapper.Map<List<Base_DepartmentOutput>>(list);
                return result.Success(Base_DepartmentOutputs, pageModel.TotalCount);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Base_DepartmentController--GetList:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 获取唯一
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultMsg<Base_DepartmentOutput>> GetTheOne(string id)
        {
            ResultMsg<Base_DepartmentOutput> result = new ResultMsg<Base_DepartmentOutput>();
            try
            {
                var model = await _Base_Department.GetByIdAsync(id);
                var dao = _mapper.Map<Base_DepartmentOutput>(model);
                return result.Success(dao);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Base_DepartmentController--GetTheOne:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultMsg<string>> Del(string id)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                await _Base_Department.DeleteByIdAsync(id);
                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Base_DepartmentController--Del:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }

        /// <summary>
        /// 设置部门列表(树结构)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultMsg<List<GetDepartmentTreeOutput>>> GetDepartmentTreeList()
        {
            ResultMsg<List<GetDepartmentTreeOutput>> result = new ResultMsg<List<GetDepartmentTreeOutput>>();
            try
            {
                string parentId = "0";

                var allLst = await _Base_Department.GetListAsync(x => !x.FDeleted);

                // 分类处理
                var menuPageList = allLst.Select(x => new GetDepartmentTreeOutput
                {
                    Id = x.Id,
                    Name = x.FName,
                    ParentId = x.FParentId,
                }).ToList();
                // 构建树结构
                var treeList = TreeHelper.BuildTree(menuPageList, parentId);

                return result.Success(treeList);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Base_DepartmentController--GetMenuTreeList:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
    }
}
